home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / corewars / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-14  |  2.1 KB  |  140 lines

  1. /* main program for MARS interpreter
  2.    As usual, this does not do anything important.
  3.    Just calls all the proper subroutines in the right order and
  4.    opens files.
  5.    -- 12/16/88         --- NCP        */
  6.  
  7. #include "interp.h"
  8. #include <ctype.h>
  9. #include <stdio.h>
  10. #include <memory.h>
  11. #include <curses.h>
  12.  
  13. long    atol();
  14.  
  15. /* memory array -- called "a" for easy typing */
  16. cell    a[SIZE];
  17.  
  18. /* next execution for each player */
  19. stream    *exe[MAXPLAY];
  20.  
  21. /* returns which player won */
  22. int    play();
  23.  
  24. usage()
  25. {
  26.     printf("usage: interp cycles file1 file2\n");
  27.     printf("--- main\n");
  28. }
  29.  
  30. /* checks that every element in string is a digit */
  31. int    number(str)
  32. char    *str;
  33. {
  34.     int    i = 1;
  35.  
  36.     while (*str && i)
  37.     {
  38.         if (!isdigit(*str))
  39.             i = 0;
  40.         str++;
  41.     }
  42.  
  43.     return i;
  44. }
  45.  
  46. initialize()
  47. {
  48.     int    counter;    /* general-purpose counter */
  49.  
  50.     counter = (int) time(0);
  51. #ifndef DEBUG
  52.     initscr();        /* for "curses" library */
  53.     scrollok(stdscr, 0);
  54.     nl();
  55.     clear();
  56.     refresh();
  57. #endif
  58.  
  59. #ifdef SUN
  60.     xcore_init();
  61. #endif
  62.     srand(counter);
  63.     for (counter = 0; counter++; counter < SIZE)
  64.         memset(a[counter],0,sizeof(cell));
  65. }
  66.  
  67. main(argc, argv)
  68. int    argc;
  69. char    *argv[];
  70. {
  71.     FILE    *f;
  72.     int    errcode, result;
  73.  
  74.     initialize();        /* initialize all global variables */
  75.  
  76.     if (argc != 4)        /* too many or too few */
  77.     {
  78.         usage();
  79.         exit(1);
  80.     }
  81.  
  82.     if (!(number(argv[1])))
  83.     {
  84.         usage();
  85.         exit(1);
  86.     }
  87.  
  88.     if ((f = fopen(argv[2], "r")) == NULL)
  89.     {
  90.         printf("%s cannot be opened\n", argv[2]);
  91.         exit(1);
  92.     }
  93.  
  94.     errcode = load(f, 1);
  95.     fclose(f);
  96.     if (errcode == 1) {
  97.         printf("main: Sorry, but %s is too large to load\n",argv[2]);
  98.         exit(1);
  99.     }
  100.  
  101.  
  102.     if ((f = fopen(argv[3], "r")) == NULL)
  103.     {
  104.         printf("%s cannot be opened\n", argv[3]);
  105.         exit(1);
  106.     }
  107.  
  108.     errcode = load(f,2);
  109.     fclose(f);
  110.     if (errcode == 1) {
  111.         printf("main: Sorry, but %s is too large to load\n",argv[3]);
  112.         exit(1);
  113.     }
  114.  
  115. #ifndef DEBUG
  116.     clear();
  117. #endif
  118.  
  119.     result = play(atol(argv[1]));
  120.  
  121. #ifndef DEBUG
  122. #ifndef SUN
  123.     output(0);
  124. #endif
  125.     move(21, 0);
  126.     if (!result)
  127.         printw("nobody won!");
  128.     else
  129.         printw("%s won!", argv[result + 1]);
  130.     move(22, 0);
  131.     printw("Hit any key to continue...");
  132.     refresh();
  133.     getch(errcode);
  134.     endwin();
  135. #endif
  136. #ifdef SUN
  137.     xcore_done();
  138. #endif
  139. }
  140.